/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yuhuibear.poat.object.bean;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import yuhuibear.poat.test.entity.Log;
import yuhuibear.poat.test.entity.PersonalProfile;
import yuhuibear.poat.test.entity.User;
import static org.junit.Assert.*;

/**
 * 
 * @author yuhuibear
 */
public class BeanStructAnalyserTest {

	public BeanStructAnalyserTest() {
	}

	@BeforeClass
	public static void setUpClass() throws Exception {
	}

	@AfterClass
	public static void tearDownClass() throws Exception {
	}

	@Before
	public void setUp() {
	}

	@After
	public void tearDown() {
	}

	/**
	 * Test of scanPackage method, of class BeanStructAnalyser.
	 */
	@Test
	public void testScanPackage() throws Exception {
		System.out.println("scanPackage");
		String packageName = "yuhuibear.poat.test.entity";
		PersistentClassLoader instance = PersistentClassLoader.getInstance();
		int expResult = 5;
		int result = instance.scanPackage(packageName);
		assertEquals(expResult, result);
		expResult = 108;
		result = instance.scanPackage("com.bgIntlgn.client.entity");
		assertEquals(expResult, result);
	}

	/**
	 * Test of analyse method, of class BeanStructAnalyser.
	 */
	@Test
	public void testAnalyse() throws Exception {
		System.out.println("analyse");
		Class<User> type = User.class;
		PersistentClassLoader instance = PersistentClassLoader.getInstance();
		instance.analyse(type);
	}

	/**
	 * Test of convertExpressionToDBForm method, of class BeanStructAnalyser.
	 */
	@Test
	public void testConvertExpressionToDBForm() throws Exception {
		System.out.println("convertExpressionToDBForm");
		String exp = "SumLogs(User.id, Log.name, 'ase, 'PersonalProfile.id'(User.name.na\\'me))'Mtas User.credite ), Log.ownerId";
		PersistentClassLoader instance = PersistentClassLoader.getInstance();
		instance.analyse(User.class);
		instance.analyse(Log.class);
		instance.analyse(PersonalProfile.class);

		String expResult = "SumLogs(users.id, log222s.name, 'ase, 'personal_profiles.id'(User.name.na'me))'Mtas users.credite ), log222s.owner_id";
		String result = instance.convertExpressionToDBForm(exp);
		assertEquals(expResult, result);

		try {
			exp = "SumLogs(User.id, Log.name, 'ase, 'PersonalProfile.Id'(User.name.na\\'me))'Mtas User.credite ), Log.ownerId";
			instance.convertExpressionToDBForm(exp);
		} catch (yuhuibear.poat.exception.MissAttributesOfBean missAtr) {
			System.out
					.println("catch yuhuibear.poat.exception.MissAttributesOfBean succeed.");
		}

		exp = "User.name = 'Log.id + random(May\\'s)' and User.cmt >10 and Log.ownerId in thisIsFunction(sum(Log.id)) , User.id as ownerId";
		expResult = "users.name = 'Log.id + random(May's)' and users.commentby >10 and log222s.owner_id in thisIsFunction(sum(log222s.id)) , users.id as ownerId";
		result = instance.convertExpressionToDBForm(exp);
		assertEquals(expResult, result);

	}
}
